package com.yishe.biz.mysql;

import com.google.gson.internal.$Gson$Types;
import com.yishe.biz.BaseBiz;
import com.yishe.context.BaseContextHandler;
import com.yishe.entites.mysql.YsCaseInfo;
import com.yishe.entites.mysql.YsStaffCase;
import com.yishe.entites.mysql.YsStaffCaseDetailInfo;
import com.yishe.mapper.mysql.YsCaseInfoMapper;
import com.yishe.mapper.mysql.YsStaffCaseDetailInfoMapper;
import com.yishe.mapper.mysql.YsStaffCaseMapper;
import com.yishe.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author : chusifeng
 * @version :1.0
 * @date :Create in 2021/4/11 12:00
 * @description: 课程处理类
 * @package com.yishe.biz.mysql
 * @className YsCaseInfoBiz
 */
@Service
@Slf4j
public class YsCaseInfoBiz extends BaseBiz<YsCaseInfoMapper, YsCaseInfo> {
    private static final String ERRORMSG = "该课程已存在上课记录，无法删除！";
    @Resource
    private YsStaffCaseDetailInfoMapper ysStaffCaseDetailInfoMapper;
    @Resource
    private YsStaffCaseMapper ysStaffCaseMapper;
    @Resource
    private YsCaseInfoMapper ysCaseInfoMapper;
    /**
     * 根据课程编号删除课程，需要判断课程是否已经被分配了
     */
    @Transactional(value = "mysqlTransactionManager",rollbackFor = Exception.class)
    public String deleteCase(Map<String,String> param){
        String caseNo = param.get("caseNo");
        //判断是否有上课记录
        YsStaffCaseDetailInfo ysStaffCaseDetailInfo = new YsStaffCaseDetailInfo();
        ysStaffCaseDetailInfo.setCaseNo(caseNo);
        List<YsStaffCaseDetailInfo> recoreds =  ysStaffCaseDetailInfoMapper.select(ysStaffCaseDetailInfo);
        if (recoreds.size()>0){
            return ERRORMSG;
        }
        //不存在上课记录，则删除课程，并且删除所有员工名下的该课程记录
        YsCaseInfo ysCaseInfo = new YsCaseInfo();
        ysCaseInfo.setCaseNo(caseNo);
        this.delete(ysCaseInfo);

        YsStaffCase ysStaffCase = new YsStaffCase();
        ysStaffCase.setCaseNo(caseNo);
        ysStaffCaseMapper.delete(ysStaffCase);
        return null;
    }


    public void saveCaseEdit(Map<String, Object> param) {
        ysCaseInfoMapper.uodateCase(param);

    }

    public void saveCase(Map<String, Object> param) {
        YsCaseInfo ysCaseInfo = new YsCaseInfo();
        Date now =new Date();
        ysCaseInfo.setCaseName(String.valueOf(param.get("name")));
        ysCaseInfo.setCaseDesc(String.valueOf(param.get("desc")));
        ysCaseInfo.setCaseImgPath(String.valueOf(param.get("imgPath")));
        ysCaseInfo.setCaseDist(String.valueOf(param.get("caseDist")));
        ysCaseInfo.setCasePeople(String.valueOf(param.get("casePeople")));
        ysCaseInfo.setCaseNo(CommonUtils.getRandomByLength(9));
        ysCaseInfo.setStatus("false".equals(param.get("status"))?1:0);
        ysCaseInfo.setCreateTime(now);
        ysCaseInfo.setLastUpdateTime(now);
        ysCaseInfo.setCreateId(BaseContextHandler.getUserCode());
        ysCaseInfo.setLastUpdateId(BaseContextHandler.getUserCode());
        ysCaseInfoMapper.insert(ysCaseInfo);
    }
}
